<html>
<head><meta charset="utf-8"><title>when to add a new symbol · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html">when to add a new symbol</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="226716303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226716303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226716303">(Feb 17 2021 at 19:58)</a>:</h4>
<p>is there any guidance on when something should be added to the great huge table of pre-interned symbols so that it can be used as sym::foo? currently I'm comparing a symbol against a literal string, but wondering if I should create a new interned symbol for the comparison</p>



<a name="226716357"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226716357" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226716357">(Feb 17 2021 at 19:58)</a>:</h4>
<p>generally if you're comparing against a string you should add to the interned list instead, afaik</p>



<a name="226716407"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226716407" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226716407">(Feb 17 2021 at 19:59)</a>:</h4>
<p>hmm, does that offer any speedup if you have to intern it every time?</p>



<a name="226716640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226716640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226716640">(Feb 17 2021 at 20:00)</a>:</h4>
<p>I'm confused by that question - the interning happens once, at rustc startup, and that is largely static IIRC (and in any case several hundred hash table insertions are cheap). Comparing against a string at runtime is relatively expensive.</p>



<a name="226716685"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226716685" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226716685">(Feb 17 2021 at 20:01)</a>:</h4>
<p>to clarify in my case it's a string literal I'm comparing against, so it's already living in static memory</p>



<a name="226716701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226716701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226716701">(Feb 17 2021 at 20:01)</a>:</h4>
<p>(IIRC, getting &amp;str from a symbol requires a lock today)</p>



<a name="226716725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226716725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226716725">(Feb 17 2021 at 20:01)</a>:</h4>
<p>ah interesting</p>



<a name="226716755"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226716755" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226716755">(Feb 17 2021 at 20:01)</a>:</h4>
<p>or at least TLS. in any case, it's cheaper to compare symbols than symbol to string.</p>



<a name="226716860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226716860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226716860">(Feb 17 2021 at 20:02)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116122">simulacrum</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/when.20to.20add.20a.20new.20symbol/near/226716640">said</a>:</p>
<blockquote>
<p>I'm confused by that question - the interning happens once, at rustc startup, and that is largely static IIRC (and in any case several hundred hash table insertions are cheap). Comparing against a string at runtime is relatively expensive.</p>
</blockquote>
<p>right - comparing <code>x == sym::rustdoc</code> is cheap. I'm asking about whether <code>Symbol::intern(x) == sym::rustdoc</code> is faster than <code>x == "rustdoc"</code>.</p>



<a name="226717213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226717213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226717213">(Feb 17 2021 at 20:05)</a>:</h4>
<p>oh. what is x there?</p>



<a name="226717280"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226717280" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226717280">(Feb 17 2021 at 20:05)</a>:</h4>
<p>if x is &amp;str it seems.. obvious that interning won't give you any wins? doing strictly more work</p>



<a name="226717385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226717385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226717385">(Feb 17 2021 at 20:06)</a>:</h4>
<p>that was my question, yeah</p>



<a name="226717387"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226717387" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226717387">(Feb 17 2021 at 20:06)</a>:</h4>
<p>the point is that you generally - in the compiler anyway - don't have &amp;str, you already have an Ident or Symbol so you're not calling intern there</p>



<a name="226717409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226717409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226717409">(Feb 17 2021 at 20:06)</a>:</h4>
<p>so then <span class="user-mention" data-user-id="256342">@bstrie</span> my question for you is if you have a &amp;str or a <code>Symbol</code></p>



<a name="226717417"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226717417" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226717417">(Feb 17 2021 at 20:06)</a>:</h4>
<p>it's been called once during parsing</p>



<a name="226717466"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226717466" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226717466">(Feb 17 2021 at 20:07)</a>:</h4>
<p>they already said they have a symbol: "a symbol against a literal string"</p>



<a name="226717501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226717501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226717501">(Feb 17 2021 at 20:07)</a>:</h4>
<p>ah ok, sorry for the noise</p>



<a name="226717577"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20add%20a%20new%20symbol/near/226717577" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20add.20a.20new.20symbol.html#226717577">(Feb 17 2021 at 20:07)</a>:</h4>
<p>yes the type I have is an actual Symbol, I didn't intern this myself, who knows where it's from :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>